﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
    public class MMarketItemDAL
    {
        public static int MbuyDetail_Add(MbuyDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("MbuyDetail_Add", obj.MMID, obj.ItemName, obj.BuyNum, obj.BuyPrice, obj.BuyDate, obj.SupplierName);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static IDataReader MbuyDetail_GetByMMID(int MMID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("MbuyDetail_GetByMMID", MMID);
            return DB.ExecuteReader(dbCommand);
	    }

        public static DataTableCollection MbuyDetail_GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("MbuyDetail_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String,whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTableCollection dt=  DB.ExecuteDataSet(dbCommand).Tables;
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static int Add(MMarketItemInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("MMarketItem_Add", obj.ItemName, obj.Price, obj.SupplierName, obj.InHold, obj.StartStock, obj.AllBuyNum, obj.AllSellNum,obj.DisplayNum);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int MMID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("MMarketItem_Delete", MMID);
           
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetByMMID(int MMID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("MMarketItem_GetByMMID", MMID);
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("MMarketItem_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String,whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(MMarketItemInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("MMarketItem_Update", obj.MMID, obj.ItemName, obj.Price, obj.SupplierName, obj.InHold, obj.StartStock, obj.AllBuyNum, obj.AllSellNum, obj.DisplayNum);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static int MSellDetail_Add(MSellDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("MSellDetail_Add", obj.MMID, obj.ItemName, obj.SellNum, obj.SellPrice, obj.SellDate, obj.AgentName, obj.Consignee, obj.ConsigneeTel, obj.ExpressMode, obj.InvoiceNum, obj.IsSell, obj.SellTotalMoney);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static IDataReader MSellDetail_GetByMMID(int MMID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("MSellDetail_GetByMMID", MMID);
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTableCollection MSellDetail_GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("MSellDetail_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String,whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
    }
}
